﻿using System;
using uForum.Library;

namespace uForum.Businesslogic.Services
{
	public class PostingService
	{
		private readonly AuthorizationService _auth;

		public PostingService()
		{
			_auth = new AuthorizationService();
		}
		public string NewTopic(int forumId, string title, string body)
		{
			

			Rights rights = _auth.GetRights(forumId);

			if (rights.CanPost)
			{
				Topic t = Topic.Create(forumId, Utills.Sanitize(title), Utills.Sanitize(body), _auth.GetUserId());

				return Xslt.NiceTopicUrl(t.Id);
			}
			else
			{
				return "0";
			}

		}

		public string NewComment(int topicId, int itemsPerPage, string body)
		{

			int forumId = new Topic(topicId).ParentId;
			Rights rights = _auth.GetRights(forumId);

			if (rights.CanPost)
			{
				Comment c = Comment.Create(topicId, Utills.Sanitize(body), _auth.GetUserId());

				return Xslt.NiceCommentUrl(c.TopicId, c.Id, itemsPerPage);
			}

			return "0";
		}

		public bool DeleteComment(int commentId)
		{
			var comment = new Comment(commentId);
			if (!comment.Id.Equals(0))
			{
				int topicId = comment.TopicId;
				int forumId = new Topic(topicId).ParentId;
				Rights rights = _auth.GetRights(forumId);

				if (rights.CanAdmin)
				{
					comment.Delete();
					return true;
				}
			}
			return false;
		}

		public bool DeleteTopic(int topicId)
		{
			var topic = new Topic(topicId);
			if(!topic.Id.Equals(0))
			{
				int forumId = topic.ParentId;
				Rights rights = _auth.GetRights(forumId);
				if(rights.CanAdmin)
				{
					topic.Delete();
					return true;
				}
			}
			return false;
		}
	}
}
